package com.ncu.cloundeoffice.controller;

import com.ncu.cloundeoffice.pojo.ResponseResult;
import com.ncu.cloundeoffice.pojo.TAdmin;
import com.ncu.cloundeoffice.service.TAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.channels.MulticastChannel;
import java.util.Date;
import java.util.Map;

@RestController
public class TAdminInfoController {

    @Autowired
    private TAdminService tAdminService;


    /**
     * 修改用户信息
     * @param tAdmin
     * @param authentication
     * @return
     */
    @PutMapping("/admin/info")
    public ResponseResult updateTAdminInfo(@RequestBody TAdmin tAdmin, Authentication authentication){
        int count = tAdminService.updateTAdminInfo(tAdmin);
        if(count == 1){
            SecurityContextHolder.getContext().setAuthentication(new
                    UsernamePasswordAuthenticationToken(tAdmin,null,authentication.getAuthorities()));
            return ResponseResult.success("更新成功");
        }
        return  ResponseResult.error("更新失败");
    }


    /**
     * 修改密码
     * @param info
     * @return
     */
    @PutMapping("/admin/pass")
    public ResponseResult updatePassword(@RequestBody Map<String, Object> info){
        String oldPassword = (String) info.get("oldPass");
        String pass = (String) info.get("pass");
        Integer adminId = (Integer) info.get("adminId");
        return tAdminService.updatePass(oldPassword,pass,adminId);
    }


    @PostMapping("/admin/userFace")
    public ResponseResult updateUserFace(MultipartFile file,@RequestParam("id") int adminId){
        if(file.isEmpty()|| file == null){
            return ResponseResult.error("头像上传失败");
        }
        String filename = file.getOriginalFilename();
        //C:\Users\28072\Pictures\test
        //http://127.0.0.1:8080/img/TAdmin/169467299f68a44462bda66a1322d4ac.jpg
        //数据库存的格式
        long time = new Date().getTime();
        filename = time+ "." +filename.split("\\.")[1];
        String storeAddress = "http://127.0.0.1:8080/img/TAdmin/" + filename;
        String fileAddress = "C:"+System.getProperty("file.separator")
                +"Users"+System.getProperty("file.separator")+"28072"+System.getProperty("file.separator")+"Pictures"+
                System.getProperty("file.separator")+"test";
        File files = new File(fileAddress);
        if (!files.exists()){
            files.mkdir();
        }
        String filesAddress = fileAddress + System.getProperty("file.separator") + filename;
        File dest = new File(filesAddress);

        try {
            file.transferTo(dest);
            int count = tAdminService.updateUserFace(adminId,storeAddress);
            if(count == 1){
                return ResponseResult.success("头像上传成功");
            }
        } catch (IOException e) {
            e.printStackTrace();

        }
        return ResponseResult.error("头像上传失败");
    }
}
